/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package ar.com.remiseria.dao;

import ar.com.remiseria.utils.Constantes;
import ar.com.remiseria.utils.HibernateUtils;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;

/**
 *
 * @author mcvalls
 */
public class GenericDAO {
    
    public <T> List<T> getAll(Class<T> type) throws HibernateException {
        Session session = HibernateUtils.getSessionFactory().getCurrentSession();
        
        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(type);
        
        List<T> allObjects = (List<T>)criteria.list();
        tx.commit();
        return allObjects;
    }
    
    public <T> T saveNew(Object obj) throws HibernateException{
        Session session = HibernateUtils.getSessionFactory().getCurrentSession();
        
        Transaction tx = session.beginTransaction();
        
        session.save(obj);
        session.flush();
        
        tx.commit();
        
        return (T) obj;
    }
    
    public <T> T update(Object obj) throws HibernateException{
        Session session = HibernateUtils.getSessionFactory().getCurrentSession();
        
        Transaction tx = session.beginTransaction();
        
        session.update(obj);
        session.flush();
        
        tx.commit();
        
        return (T) obj;
    }
    
    public void  delete(Object obj) throws HibernateException{
        Session session = HibernateUtils.getSessionFactory().getCurrentSession();
        
        Transaction tx = session.beginTransaction();
        
        session.delete(obj);
        session.flush();
        
        tx.commit();
    }
    
    public <T> List<T> getAll(Class<T> type, int start) throws HibernateException {
        Session session = HibernateUtils.getSessionFactory().getCurrentSession();
        
        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(type);
        criteria.setFirstResult(start);
        criteria.setMaxResults(Constantes.PAG_MAX_RESULTS);
        
        List<T> allObjects = (List<T>)criteria.list();
        tx.commit();
        return allObjects;
    }
    
    public <T> int getCount(Class<T> type) throws HibernateException {
        Session session = HibernateUtils.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        
        Criteria criteria = session.createCriteria(type);
        criteria.setProjection(Projections.rowCount());
        
//        Integer count = (Integer) criteria.uniqueResult();
        String countStr = String.valueOf(criteria.uniqueResult());
        int count = Integer.valueOf(countStr);
        return count;
        
    }
}
